const path = require('path');
const webpack = require('webpack');
//引入html-webpack-plugin 打包index.html
const HtmlWebpackPlugin = require('html-webpack-plugin');

//清理dist文件夹
const {CleanWebpackPlugin} = require('clean-webpack-plugin');

//打包样式
const extractTextWebpackPlugin = require('extract-text-webpack-plugin');

//压缩js
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

//压缩css
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = {
  entry: './src/main.js',

  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {}
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader'
      },
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.output.publicPath = './';
  module.exports.output.filename = 'js/[name].[chunkhash:8].js';    //定义输出的js
  module.exports.plugins = (module.exports.plugins || []).concat([
    new CleanWebpackPlugin(),    //清理打包文件夹

    new extractTextWebpackPlugin('styles.[contentHash:8].css'),    //输出 styles.css文件

    new webpack.optimize.ModuleConcatenationPlugin(),
    new webpack.optimize.CommonsChunkPlugin({
      //分离出类库
      name: 'vendor'
    }),
    new OptimizeCssAssetsPlugin({
      //css压缩
      assetNameRegExp: /.css$/g,
      cssProcessor: require('cssnano'),
      cssProcessorOptions: {
        discardComments: {
          removeAll: true
        }
      },
      canPrint: true
    }),

    new webpack.optimize.CommonsChunkPlugin({
      name: 'runtime'
    }),

    new webpack.BannerPlugin('版权所有，翻版必究'),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    }),
    //生成index.html的插件
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,//压缩空白
        removeAttributeQuotes: true,//删除属性双引号
        removeScriptTypeAttributes:true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
      },
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
  ])
}
